﻿using Timer = System.Windows.Forms.Timer;

namespace AntdTest.Forms
{
    public partial class Clock : AntdUI.Window
    {
        readonly Timer timer = new() { Interval = 200 };

        public Clock()
        {
            InitializeComponent();

            Text = $"{Globals.App.Name} - 时钟";
            Size = new Size(350, 120);
            FormBorderStyle = FormBorderStyle.FixedSingle;
            Resizable = false;
            StartPosition = FormStartPosition.Manual;
            ShowInTaskbar = false;
            TopMost = true;
            labelClock.Font = new Font("微软雅黑", 40F, FontStyle.Bold);

            Shown += Clock_Shown;
            VisibleChanged += Clock_VisibleChanged;
            FormClosing += Clock_FormClosing;

            timer.Tick += Timer_Tick;

            labelClock.MouseHover += new EventHandler((s, e) =>
            {
                labelClock.Font = new Font("微软雅黑", 42F, FontStyle.Bold);
            });
            labelClock.MouseLeave += new EventHandler((s, e) =>
            {
                labelClock.Font = new Font("微软雅黑", 40F, FontStyle.Bold);
            });
        }

        private void Clock_Shown(object? sender, EventArgs e)
        {
            UpdateClock();
        }

        private void Clock_VisibleChanged(object? sender, EventArgs e)
        {
            if (Visible)
            {
                Helpers.Antd.InheritTheme(this);

                timer.Start();

                if (Globals.App.Pendants.Contains(this))
                {
                    Globals.App.Pendants.Remove(this);
                }
                Globals.App.Pendants.Add(this);
            }
            else
            {
                timer.Stop();

                Globals.App.Pendants.Remove(this);
            }
        }

        private void Clock_FormClosing(object? sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        private void Timer_Tick(object? sender, EventArgs e)
        {
            UpdateClock();
        }

        private void UpdateClock()
        {
            labelClock.Text = $"{DateTime.Now:HH:mm:ss}";
        }
    }
}